/*****************************************************************************************************
STATA do-file that generates the tables and figures used in the paper: 

Public Sector Employment and Voter Turnout
Benny Geys and Rune J. Sørensen

published in the American Politial Science Review 
*****************************************************************************************************/
clear all
cd "E:\Users\FAG89001\OneDrive - BI Norwegian Business School (BIEDU)\PublicSectorTurnout\Tables"  /* Directory for output files */
use "G:\BI_Research\0208_Listekandidater_2019\OutputDta\Vote20132019.dta"							
replace 	Sector	=	0 	if Sector==.
gen 		Work=. 			if Sector==0 | Sector==. /* Define Work as missing when Sector=0 */
replace Work=0 
replace Work=1 if knr_res==knr_work

* Redefine not employed by people with wage income less than 1.000 NOK per year
replace Sector=0 if wlonn< 1000

* Redefine sector (combining municipal and county government employees)
replace Sector=3 if Sector==4  									/* Combining municipal and county employees to one category */
label define NewSectorLab 0 "No occupation" 1 "Private sector" 2 "Central government" 3 "Local government" 5 "Non-profit sector"
label values Sector NewSectorLab

* Number of occurencies per person
gen In=1 if Vote!=.
bysort w19_0078_lopenr_person: egen NObs=sum(In) 						/* Count number of occurencies by person */
by 				w19_0078_lopenr_person, sort: gen nvals = _n == 1 		/* Tag first occurence of each person */
drop if NObs>=5
tab NObs year
tab nvals year
unique w19_0078_lopenr_person

* Finding number of municipalities with full count
by kommnr year, sort: gen nkommnr=_n ==1 if Vote!=.
bysort year: count if nkommnr==1  

* Previous sector for currently retired persons
sort w19_0078_lopenr_person year
by w19_0078_lopenr_person: gen Sector_lag1=Sector[_n-1] 		/* Define Sector in previous election */
by w19_0078_lopenr_person: gen Sector_lag2=Sector[_n-2] 		/* Define Sector in previous election */
by w19_0078_lopenr_person: gen Sector_lag3=Sector[_n-3] 		/* Define Sector in previous election */
by w19_0078_lopenr_person: gen Sector_lag4=Sector[_n-4] 		/* Define Sector in previous election */
by w19_0078_lopenr_person: gen Sector_lag5=Sector[_n-5] 		/* Define Sector in previous election */

gen 		Sector_ext	=	Sector 
replace 	Sector_ext	=	Sector_lag1 if Sector==0 | Sector==.
replace 	Sector_ext	=	Sector_lag2 if Sector==0 | Sector==.
replace 	Sector_ext	=	Sector_lag3 if Sector==0 | Sector==.
replace 	Sector_ext	=	Sector_lag4 if Sector==0 | Sector==.

label define Sector_ext 0 "Not defined" 1 "Private sector" 2 "Central government" 3 "Local government" 5 "Non-profit sector"
label values Sector_ext NewSectorLab

* Social security income as share of total income
replace wskplovf	=	. 	if wskplovf<0
replace wsaminnt	=	. 	if wsaminnt<=0
gen Soc_inc			=	wskplovf/wsaminnt
lab var Soc_inc "Share of income from social security"
* Retirement defined by income
gen 		Retired_inc		=	0 if Soc_inc<0.5
replace 	Retired_inc		=	1 if Soc_inc>=0.5

* Encoding selected variables
encode bu_gruppe, gen(Ed_level_num)
replace Ed_level_num=9 if Ed_level_num==.  	/* Define education level for missing categories */
encode invkat, gen(invkat_num)
replace invkat_num=9 if invkat_num==.		/* Define immigration type for missing categories */
unique kommnr if Vote!=., by(year)

* Define type of election as string for figure 
gen 	ElType	=	"National election" if year==2013 | year==2017
replace ElType	=	"Local elections" 	if year==2015 | year==2019

***********************************  OUTPUT FIGURES AND TABLES **************************************************************************************
**** Figure 1  Voter turnout 2013-2019
set scheme s1mono
binscatter Vote year, by(Sector) line(connect)  color(%30gray) msymbol(circle_hollow triangle_hollow diamond_hollow square_hollow dot) ///
xlabel(2013(2)2019) ylabel(0.5(0.1)1.0) xtitle("Election year") ytitle("Voter turnout") graphregion(color(white)) ///
legend(order(1 "Not working" 2 "Private sector" 3 "Central government" 4 "Local government" 5 "Non-profit sector") ///
col(3) size(small) title("Employment sector", size(small)))
graph save Figure1.gph, replace 
graph export Figure1.tiff, as(tif) replace

*** Figure 2 Turnout and occupational sector 
*** Corresponding to: Table A5 (column 1) Voter turnout, employment sector and workplace location 
fvset base 1 Sector  				/* Set reference group to private sector */
gen		NotWorking=0
replace NotWorking=1 	if Sector==0 
gen		Private=0
replace Private=1 		if Sector==1
gen		CentralGov=0
replace CentralGov=1 	if Sector==2
gen		LocalGov=0
replace LocalGov=1 		if Sector==3
gen		NonProfit=0
replace NonProfit=1 	if Sector==5

* Individual fixed effects (Only control for education levels)
reghdfe Vote 	NotWorking CentralGov LocalGov NonProfit, absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
estimates store Individual__FE1
outreg2 using 	TableA5.doc, replace alpha(0.001, 0.01, 0.05) nocon ctitle(All) addtext(Fixed effects, Ind., Controls, No)

*** Output of Figure 2
coefplot Individual__FE1, aseq drop(_cons) xline(0, lpattern(dash) lcolor(black)) msymbol(circle_hollow) graphregion(color(white)) ///
		 rename(NotWorking="Not working" CentralGov="Central gov." LocalGov="Local gov." NonProfit="Non-profit") 
graph save Figure2.gph, replace  
graph export Figure2.tiff, as(tif) replace

*** Figure 3 Turnout, occupational sector and work location
replace Work=0 if Sector==0
reghdfe Vote NotWorking CentralGov LocalGov NonProfit c.Private#c.Work c.CentralGov#c.Work c.LocalGov#c.Work c.NonProfit#c.Work, absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
estimates store Individual_FE2  

coefplot  	Individual_FE2, 	drop(_cons)  xline(0, lpattern(dash) lcolor(black)) msymbol(circle_hollow) graphregion(color(white))    ///
			rename(NotWorking="Not working" CentralGov="Central gov." LocalGov="Local gov." NonProfit="Non-profit" ///
			c.Private#c.Work="Private sector*Home mun." c.CentralGov#c.Work="Central gov.*Home mun." c.LocalGov#c.Work="Local gov.*Home mun." c.NonProfit#c.Work="Non-profit*Home mun.") 
graph save Figure3.gph, replace  
graph export Figure3.tiff, as(tif) replace


*** Table A5 (column 2) Voter turnout, employment sector and workplace location 	 
reghdfe Vote NotWorking CentralGov LocalGov NonProfit Work c.CentralGov#c.Work c.LocalGov#c.Work c.NonProfit#c.Work if Sector>=0, absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
outreg2 using TableA5.doc, append alpha(0.001, 0.01, 0.05) nocon   ctitle(All) addtext(Fixed effects, Id., Controls, No)

*** Table A5 cont. -- Separate resuls for voter turnout in local elections
reghdfe Vote 	NotWorking CentralGov LocalGov NonProfit if ElType=="Local elections", absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
outreg2 using TableA5.doc, append alpha(0.001, 0.01, 0.05) nocon   ctitle(Local) addtext(Fixed effects, Id., Controls, No)
reghdfe Vote NotWorking CentralGov LocalGov NonProfit Work c.CentralGov#c.Work c.LocalGov#c.Work c.NonProfit#c.Work if ElType=="Local elections", absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
outreg2 using TableA5.doc, append alpha(0.001, 0.01, 0.05) nocon   ctitle(Local) addtext(Fixed effects, Id., Controls, No)

*** Table A5 cont. -- Separate resuls for voter turnout in national elections
reghdfe Vote 	NotWorking CentralGov LocalGov NonProfit if ElType=="National election", absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
outreg2 using TableA5.doc, append alpha(0.001, 0.01, 0.05) nocon   ctitle(National) addtext(Fixed effects, Id., Controls, No)

reghdfe Vote NotWorking CentralGov LocalGov NonProfit Work c.CentralGov#c.Work c.LocalGov#c.Work c.NonProfit#c.Work if ElType=="National election", absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res) 
outreg2 using TableA5.doc, append alpha(0.001, 0.01, 0.05) nocon   ctitle(National) addtext(Fixed effects, Id., Controls, No)

*** Table A6 (with controls, no figure)
reghdfe Vote NotWorking CentralGov LocalGov NonProfit alderu kjonn i.Ed_level_num i.invkat_num, absorb(i.year#i.knr_res) vce(cluster knr_res) 
outreg2 using TableA6.doc, replace alpha(0.001, 0.01, 0.05) nocon   ctitle(National) addtext(Fixed effects, Id., Controls, Yes)
reghdfe  Vote NotWorking CentralGov LocalGov NonProfit Work c.CentralGov#c.Work c.LocalGov#c.Work c.NonProfit#c.Work alderu kjonn i.Ed_level_num i.invkat_num, absorb(i.year#i.knr_res) vce(cluster knr_res) 
outreg2 using TableA6.doc, append alpha(0.001, 0.01, 0.05) nocon   ctitle(National) addtext(Fixed effects, Id., Controls, Yes)

*** Figure 4 Voter turnout before and after retirement
*** Table A7 Voter turnout, employment sector, and retirement
gen		Retired	=	0 if Soc_inc<0.1 & alderu<=67 
replace Retired	=	1 if Soc_inc>0.9 & alderu>67 
gen		Private_ext=0		if Sector_ext!=.
replace Private_ext=1 		if Sector_ext==1
gen		CentralGov_ext=0	if Sector_ext!=.
replace CentralGov_ext=1 	if Sector_ext==2
gen		LocalGov_ext=0		if Sector_ext!=.
replace LocalGov_ext=1 		if Sector_ext==3
gen		NonProfit_ext=0		if Sector_ext!=.
replace NonProfit_ext=1 	if Sector_ext==5

fvset base 0 Retired   	
reghdfe Vote CentralGov_ext LocalGov_ext NonProfit_ext c.Private_ext#c.Retired  c.CentralGov_ext#c.Retired c.LocalGov_ext#c.Retired c.NonProfit_ext#c.Retired if Sector>=1, ///
absorb(i.year#i.knr_res Ed_level_num  w19_0078_lopenr_person) vce(cluster knr_res)
estimates store Individual_FE3
outreg2 using TableA7.doc, replace alpha(0.001, 0.01, 0.05) nocon ctitle(All) addtext(Fixed effects, Ind., Controls, Yes)
** Output of Figure 4  
coefplot  	Individual_FE3, 	drop(_cons)  xline(0, lpattern(dash) lcolor(black)) msymbol(circle_hollow) graphregion(color(white)) ///
			rename(CentralGov_ext="Central gov." LocalGov_ext="Local gov." NonProfit_ext="Non-profit" c.Private_ext#c.Retired="Private sector*Retired" ///
			c.CentralGov_ext#c.Retired="Central gov.*Retired" c.LocalGov_ext#c.Retired="Local gov.*Retired" c.NonProfit_ext#c.Retired="Non-profit*Retired") 
graph save Figure4.gph, replace 
graph export Figure4.tiff, as(tif) replace
***********************************  ONLINE APPENDIX ONLY **************************************************************************************

**** Figure A1 - Retirement and age
set scheme s1mono
binscatter Retired_inc alderu if Sector_ext>=1 & alderu<=80, by(Sector_ext) line(connect) discrete ///
ytitle("Retired") xtitle("Age") color(%30gray) graphregion(color(white)) ///	
legend(lab(1 "Private sector") lab(2 "Central government") lab(3 "Local government") lab(4 "Nonprofit") size(small)) xline(62 67, lpattern(dash))
graph save FigureA1.gph, replace 
graph export FigureA1.tiff, as(tif) replace
**** Figure A2 The retirement effect
preserve 
gen		RR	=	0 if Soc_inc<0.1 & alderu<=67 
replace RR	=	1 if Soc_inc>0.9 & alderu>67 

collapse (mean) Vote (count) V_sum=Vote, by(alderu Sector_ext RR)
sum V_sum, d
keep if alderu>=20 & alderu<=80 & V_sum>=500
set scheme s1mono
twoway (connect Vote alderu if Sector_ext==1 & RR==0, msize(vsmall) msymbol(circle)) 		|| (connect Vote alderu if Sector_ext==1 & RR==1, msize(vsmall) msymbol(circle_hollow))  ||  ///
	   (connect Vote alderu if Sector_ext==2 & RR==0, msize(vsmall) msymbol(triangle)) 		|| (connect Vote alderu if Sector_ext==2 & RR==1, msize(vsmall) msymbol(triangle_hollow)) ||   ///
	   (connect Vote alderu if Sector_ext==3 & RR==0, msize(vsmall) msymbol(square)) 		|| (connect Vote alderu if Sector_ext==3 & RR==1, msize(vsmall) msymbol(square_hollow) 	///	
legend(lab(1 "Private sector") lab(2 "Private sector, retired") lab(3 "Central government") lab(4 "Central government, retired") ///
lab(5 "Local government") lab(6 "Local government, retired") size(small)) xline(67, lpattern(dash)) ///
xtitle("Age") ytitle("Voter turnout") graphregion(color(white)))
graph save FigureA2.gph, replace 
graph export FigureA2.tiff, as(tif) replace
restore 

**** TABLE A1 - SAMPLE DESCRIPTION 
unique kommnr if Vote!=., by(year) /* Number of unique municipalities by year */
asdoc table year if year>=2013, title(Table A1) contents(count Vote) replace save(TableA1.doc)

**** TABLE A2 - DESCRIPTIVES - Turnout by sector & year
asdoc table Sector year if year>=2013 & Vote!=., title(Table A2) contents(mean Vote count Vote) replace save(TableA2.doc)

**** Table A3 - DESCRIPTIVES SHIFTS IN SECTOR
gen 		Sector_ch=0 if Sector_lag1==Sector
replace 	Sector_ch=1 if Sector_lag1!=Sector & Sector!=. & Sector_lag1!=.
* tab Sector_ch year if Vote!=., col 
asdoc table year if year>=2013 & Vote!=., title(Table A3) contents(mean Sector_ch count Sector_ch) replace save(TableA3.doc)


**** Table A4 - DESCRIPTIVES - work in home municipality vs outside - local elections only
* tabulate Work Sector if Vote>=0 & ElType=="Local elections", sum(Vote) mean freq
asdoc table Work Sector if year>=2013 & Vote!=. & (year==2015 | year==2019),  title(Table A4) contents(mean Vote count Vote) replace save(TableA4.doc)
